<?php
namespace app\admin\controller\v1;
use app\admin\Adminbase;
use think\Db;

class Home extends Adminbase
{
	
	/**
	* 获取主页数据
	* /admin/v1/Home/getData
	* 需要登录 
	* @is_need_login true
	*/
	public function getData(){

	    $request =  request();

		try {

			$data = [

                //会员总数
				"total_user_vip_num"=>model('user_vip')->alias('a')
                    ->join('xw_user b','b.id=a.uid','left')
                    ->where('b.is_enable',1)
                    ->where('b.shop_id',$request->shop_id)
                    ->count(),

                //订单总数
                "total_order_num"=>model('order')
                    ->where('is_del',0)
                    ->where('status','>=',0)
                    ->where('is_pay',1)
                    ->where('shop_id',$request->shop_id)
                    ->count(),

                //成交金额
                "total_order_money"=>model('order')
                    ->where('is_del',0)
                    ->where('status','>=',0)
                    ->where('is_pay',1)
                    ->where('shop_id',$request->shop_id)
                    ->sum('pay_price'),

                //商品总数
                "total_goods_num"=>model('goods')
                    ->where('status','<>','-1')
                    ->where('is_del',0)
                    ->where('shop_id',$request->shop_id)
                    ->count(),

                //今日订单数
                "total_order_today_num"=>model('order')
                    ->where('is_del',0)
                    ->where('status','>=',0)
                    ->where('is_pay',1)
                    ->whereTime('add_time','today')
                    ->where('shop_id',$request->shop_id)
                    ->count(),

                //昨日订单数
                "total_order_yesterday_num"=>model('order')
                    ->where('is_del',0)
                    ->where('status','>=',0)
                    ->where('is_pay',1)
                    ->where('shop_id',$request->shop_id)
                    ->whereTime('add_time','yesterday')
                    ->count(),

                //近7天订单数
                "total_order_week_num"=>model('order')
                    ->where('is_del',0)
                    ->where('status','>=',0)
                    ->where('is_pay',1)
                    ->where('shop_id',$request->shop_id)
                    ->whereTime('add_time','week')
                    ->count(),

                //本月订单数
                "total_order_month_num"=>model('order')
                    ->where('is_del',0)
                    ->where('status','>=',0)
                    ->where('is_pay',1)
                    ->where('shop_id',$request->shop_id)
                    ->whereTime('add_time','month')
                    ->count(),

                //本月成交额和本月订单总数
                "total_sum_and_num"=>$this->getMonthData($request->shop_id),

                //店铺钱包金额
                "total_shop_wallet"=>$this->getshopmoney($request->shop_id)


            ];
			return $this->ReSucess($data);
        } catch (\Exception $e) {
            return $this->ReError($e->getMessage());
            //throw new \think\Exception( $e );
		}
	}

    /**
     * 获取主页本月成交额和本月订单数
     * /admin/v1/Home/getMonthData
     * 需要登录
     * @is_need_login true
     */

    public function getMonthData($shop_id){

        try {

            $month=date("m");
            $year = date("Y");

             //$max_day = cal_days_in_month(CAL_GREGORIAN, $month, $year);   //当月最后一天
             
             $BeginDate=date('Y-m-01', strtotime(date("Y-m-d")));
             $max_day=date('d', strtotime("$BeginDate +1 month -1 day"));


            $min = $year.'-'.$month.'-01 00:00:00';
            $max = $year.'-'.$month.'-'.$max_day.' 23:59:59';
            $sql = "select t.add_time,sum(t.pay_price) 
                    as amount,count(*) as total_num from (select date_format(add_time,'%e') 
                    as add_time,pay_price from xw_order where add_time 
                    between '{$min}' and '{$max}' and is_del=0 and status>=0 and is_pay=1 and shop_id=$shop_id) t 
                    group by t.add_time 
                    order by t.add_time";

            $result = Db::query($sql);

            $month_days=date('t');

            $result_census=[];
            for ($i=1;$i<=$month_days;$i++){

                if(count($result)){

                    for($j=0;$j<count($result);$j++){
                        if($i==$result[$j]['add_time']){
                            $result_census[$i]['amount']=$result[$j]['amount'];
                            $result_census[$i]['total_num']=$result[$j]['total_num'];
                            break;
                        }else{
                            $result_census[$i]['amount']=0;
                            $result_census[$i]['total_num']=0;
                        }
                    }

                }else{

                        $result_census[$i]['amount']=0;
                        $result_census[$i]['total_num']=0;
                }

            }

            return $result_census;

        } catch (\Exception $e) {
            return $this->ReError($e->getMessage());
        }

    }

    /**
     * 获取店铺数
     * /admin/v1/Home/getMonthData
     * 需要登录
     * @is_need_login true
     */

    public function getshopmoney($shop_id){

        try {
            $result_shop=model('shop')
                ->field("balance,lock_balance")
                ->where('is_del',0)
                ->where('status',1)
                ->where('id',$shop_id)
                ->find();

            return $result_shop['balance']-$result_shop['lock_balance'];

        } catch (\Exception $e) {
            return $this->ReError($e->getMessage());
        }

    }
	
}



